const app = getApp();
Page({
  data: {
    currentActive: 0,
    showModal: false,
    showGuid: app.globalData.loadTime > 0 ? true : false,
    placeholder: '布展中',
    templateList: [
      {
        num: '1',
        img: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        title: '../../images/first_n.png',
        title2: '../../images/first_s.png',
        title3: '../../images/first_s_en.png'
      },
      {
        num: '2',
        img: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
        title: '../../images/second_n.png',
        title2: '../../images/secon_s.png',
        title3: '../../images/second_s_en.png'
      },
      {
        num: '3',
        img: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
        title: '../../images/third_n.png',
        title2: '../../images/third_s.png',
        title3: '../../images/third_s_en.png'
      },
      {
        num: '4',
        img: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
        title: '../../images/fourth_n.png',
        title2: '../../images/fourth_s.png',
        title3: '../../images/fourth_s_en.png'
      },
      {
        num: '5',
        img: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
        title: '../../images/fifth_n.png',
        title2: '../../images/fifth_s.png',
        title3: '../../images/fifth_s_en.png'
      },
      {
        num: '6',
        img: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
        title: '../../images/six_n.png',
        title2: '../../images/six_s.png',
        title3: '../../images/six_s_en.png',
      },
    ],
    inputVal: "",
    inputShowed: false,
    imageUrl:'',
    title:'',
    detail:'',
    logo:[],
    position:'',
    national:[],
    language:'zh',
    enInfo: {},
    showSearch:false,
    language: app.globalData.lang
  },
  onLoad: function (options) {
    //调用app中的函数
    console.log(app.globalData.loadTime > 0);
    this.setData({
      showGuid: app.globalData.loadTime > 0 ? true : false,
    });
    var lang = app.globalData.lang;
    if (lang != "zh") {
      wx.setNavigationBarTitle({
        title: 'Country Pavilion'
      })
    }
    const that=this;
    if (lang == 'en') {
      let _enfo = that.data.enInfo;
      _enfo.selectedBlock = 'Selected Block';
      _enfo.entrance = 'Entrance';
      _enfo.areaPlan = 'Area Plan';
      _enfo.area = 'Select pavilion map in the area'; 
      that.setData({
        enInfo: _enfo,
        placeholder: 'Preparing',
        templateList: [
          {
            num: '1',
            img: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
            title: '../../images/first_n.png',
            title2: '../../images/first_s.png',
            title3: '../../images/first_s_en.png'
          },
          {
            num: '2',
            img: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
            title: '../../images/second_n.png',
            title2: '../../images/secon_s.png',
            title3: '../../images/second_s_en.png'
          },
          {
            num: '3',
            img: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
            title: '../../images/third_n.png',
            title2: '../../images/third_s.png',
            title3: '../../images/third_s_en.png'
          },
          {
            num: '4',
            img: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
            title: '../../images/fourth_n.png',
            title2: '../../images/fourth_s.png',
            title3: '../../images/fourth_s_en.png'
          },
          {
            num: '5',
            img: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
            title: '../../images/fifth_n.png',
            title2: '../../images/fifth_s.png',
            title3: '../../images/fifth_s_en.png'
          },
          {
            num: '6',
            img: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
            title: '../../images/six_n.png',
            title2: '../../images/six_s.png',
            title3: '../../images/six_s_en.png'
          },
        ],
      })
    }
    wx.request({
      url:app.globalData.url+'national/showList',
      method:'POST',
      data:{language:lang},
      success:function(e){
          console.log(e);
          that.setData({
            national:e.data.data.companies,
            language:lang,
            imageUrl:app.globalData.imageUrl,
          });
      }
    });
  },
  clearInput: function() {
    this.setData({
      inputVal: ""
    });
  },
  showSearch:function(){
    this.setData({
      showSearch:true,
    });
  },
  hideSearch:function(){
    this.setData({
      showSearch:false,
    });
  },
  inputTyping: function(e) {
    const that=this;
    this.setData({
      inputVal:e.detail.value,
      showSearch:true,
    });
    if(e.detail.value==''){
      return;
    }
    wx.request({
      url:app.globalData.url+'national/search',
      method:'POST',
      data:{
        language:this.data.language,
        title:e.detail.value,
      },
      success:function(msg){
        var searchResult = msg.data.data.companies;
        console.log(searchResult);
        if (searchResult){
          for (var i = searchResult.length-1;i>=0 ;i--) {
            console.log(item);
            var item = searchResult[i];
            if (item.title.indexOf("澳门") != -1 || item.title.indexOf("香港") != -1 || item.title.indexOf("台湾") != -1 || item.title.indexOf("Macao") != -1 || item.title.indexOf("Hong Kong") != -1 || item.title.indexOf("Taiwan") != -1) {
              searchResult.splice(i, 1);
            }
          }
        }
        console.log(searchResult);
        that.setData({
          searchItem:msg.data.data.companies,
        });
      }
    });
  },
  shutDown: function () {
    app.globalData.loadTime = 1;
    console.log(app.globalData.loadTime);
    this.setData({
      showModal: false,
      showGuid: true,
    })
  },
  submit: function (e) {
    const that=this;
    var index = e.currentTarget.dataset.index;
    wx.request({
      url:app.globalData.url+'national/detail',
      method:'POST',
      data:{
        language:this.data.language,
        id:e.currentTarget.dataset.id,
      },
      success:function(e){
        that.setData({
          detail:e.data.data.company.detail,
          logo:e.data.data.company.logo,
          title:e.data.data.company.title,
          position:e.data.data.company.position,
          currentActive:index ? index : e.data.data.company.area,
          showModal: true,
          showSearch:false,
        });
      }
    });
  },
  changeMoban: function (e) {
    // console.log(e.currentTarget.dataset);
    this.setData({
      currentActive: e.currentTarget.dataset.current
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: app.globalData.shareTitle,
      path: app.globalData.sharePath,
      imageUrl: '../../../../images/share_img.png',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
})